package com.jxb.webfluxController.router;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.jxb.webfluxController.handler.UserHandler;

import lombok.extern.slf4j.Slf4j;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/10/23 19:46
 **/
@Configuration
@Slf4j
public class UserRouterCfg {

    // @Bean
    // public RouterFunction<ServerResponse> userRouterFunctions(UserHandler userHandler) {
    //     return RouterFunctions.route().GET("/webFlux/user/get",userHandler::getUser).build();
    // }

    //添加过滤器
    @Bean
    public RouterFunction<ServerResponse> userRouterFunctions(UserHandler userHandler) {
        return RouterFunctions.route().filter((((serverRequest, handlerFunction) -> {
            ServerRequest.Headers headers = serverRequest.headers();
            List<String> token = headers.header("token");
            if (token == null || token.isEmpty()) {
                return ServerResponse.status(HttpStatus.FORBIDDEN).build();
            }
            return handlerFunction.handle(serverRequest);
        }))).GET("/webFlux/user/get",userHandler::getUser).build();
    }
}
